<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Timedtasks extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('M_quantification');
        $this->load->model('M_quantification_Map');
        $this->load->model('M_quantification_User');
        $this->load->model('M_quantification_User_Map');
    }
	public function index()
	{
        echo 'test';exit;
		$this->load->view('welcome_message');
    }

    //每三分钟生成一次量化任务
    public function task_start_one(){
        $time = time();
        $ys = strtotime(date('Y-m')); //月初
        $ye = date('Y-m-t').' 23:59:59';
        $ye = strtotime($ye);//月末
        $ys_e = $ye - $ys;     //月末和月初的时间差
        $log = '';



        //按月生成的量化任务 未触发过的  status 1 月 2 上线  statu 1 为触犯 2 已经触发
        $tables = array('t_quantification'=>'q');
        //模板
        $moban = $this->M_quantification->getJoinList($tables,'','q.*',array('q.is_del'=>1,'q.quantification_status'=>'1','q.quantification_check_cycle'=>'1','q.quantification_post_type <='=>2,'q.quantification_start_time <='=>$time),null,null);
      //  $tables = array('t_user'=>'u','t_enterprise_dept'=>'b');
        $tables = array('t_user'=>'u');
        if(!empty($moban)){
            foreach($moban as $k=>&$v){
                //$v['start_time'] = 触发开始时间
                //月底到触发时间的差
                $v['yes_e'] = $ye - $v['quantification_start_time'];
                //量化任务的网格任务
                $v['map_list'] = $this->M_quantification_Map->getList('*',array('quantification_id'=>$v['quantification_id'],'is_del'=>1),null,null);
                //查询模板所对应的人员  1人员 2 岗位
                $v['user'] = array();
                if($v['quantification_post_type'] == 2){
                    $user = array();
                    $user = $this->M_quantification->getJoinList($tables,'','u.user_id','',null,null);

                    foreach($user  as $kk=>$vv){
                        $v['user'][] = $vv['user_id'];
                    }
                }else{
                    $v['user'] = explode(",",$v['quantification_user']);
                }


                //循环用户给每个用户任务模板
                foreach($v['user'] as $kkk=>$vvv){
                    if(empty($vvv)){
                        continue;
                    }

                    $data =array(
                        'user_id'=>$vvv,
                        'quantification_id'=>$v['quantification_id'],
                        'quantification_user_taketime'=>$v['quantification_start_time'],
                        'quantification_user_check'=>$v['quantification_check_cycle'],
                        'quantification_user_addtime'=>time(),
                        'quantification_user_status'=>1
                    );
                    //用户每月任务
                   $rid = $this->M_quantification_User->saveData($data);
                    $log .='用户每月任务模板:'.$rid.'用户:'.$vvv.'---用户每月任务模板详情';
                    //用户任务详情 根据每个任务模块的网格记录任务
                    foreach($v['map_list'] as $kkkk=>$vvvv){

                        //计算每个网格的月任务 和开始日期结束日期
                        //网格内的写的时间间隔
                        $map_time_i = ($vvvv['quantification_task_cycle_d'] * 24 * 60) + ($vvvv['quantification_task_cycle_h'] * 60);
                        $map_time_jiange = $map_time_i * 60;// 间隔时间 秒

                        $map_number =  round($v['yes_e']/$map_time_jiange);  //每个月生成任务的次数

                        //每个网格的任务插入
                        for($x=1; $x<=$map_number; $x++ ){
                            $data_v = array(
                                'quantification_user_id'=> $rid,
                                'quantification_map_id'=>$vvvv['quantification_map_id'],
                                'grid_id'=>$vvvv['grid_id'],
                                'end_time'=>$v['quantification_start_time'] + ($map_time_jiange * $x)-1,
                                'start_time'=>$v['quantification_start_time'] + ($map_time_jiange * $x) - $map_time_jiange,
                                'finish_number'=>0,
                                'number'=>$vvvv['quantification_task_number'],
                                'quantification_user_map_addtime'=>$time,
                                'user_id'=>$vvv,
                            );
                            if($x == $map_number){
                                $data_v['end_time'] = $ye;
                            }
                           $s =  $this->M_quantification_User_Map->saveData($data_v);
                            $log .='--'.$s;
                        }

                    }

                }

                //修改模板状态
                $this->M_quantification->updateData(array('quantification_status'=>2),array('quantification_id'=>$v['quantification_id']));
                $log .= '修改模板'.$v['quantification_id'].'<br/>';
            }
        }

        $log .='上线任务相关<br/><br/>';
        //按上线生成的量化任务
        //的量化任务 未触发过的  status 1 月 2 上线  statu 1 为触犯 2 已经触发
//        $tables = array('t_quantification'=>'q');
//        //模板
//        $moban = $this->M_quantification->getJoinList($tables,'','q.*',array('q.is_del'=>1,'q.quantification_check_cycle'=>'2','q.quantification_status'=>'2','q.quantification_post_type <='=>2,'q.quantification_start_time <='=>$time),null,null);
//        //  $tables = array('t_user'=>'u','t_enterprise_dept'=>'b');
//        $tables = array('t_user'=>'u');
//        if(!empty($moban)){
//            foreach($moban as $sk=>$sv){
//                //量化模板对应的网格任务
//                $sv['map_list'] = $this->M_quantification_Map->getList('*',array('quantification_id'=>$sv['quantification_id']),null,null);
//                //查询模板所对应的人员  1人员 2 岗位
//                if($sv['quantification_post_type'] == 2){
//                    $user = array();
//                    $user = $this->M_quantification->getJoinList($tables,'','u.user_id','',null,null);
//                    foreach($user  as $kk=>$vv){
//                        $sv['user'][] = $sv['quantification_user'];
//                    }
//                }else{
//                    $sv['user'] = explode(",",$sv['quantification_user']);
//                }
//                //循环用户给每个用户任务模板
//                foreach($sv['user'] as $skkk=>$svvv) {
//                    if (empty($svvv)) {
//                        continue;
//                    }
//                    $data = array(
//                        'user_id' => $svvv,
//                        'quantification_id' => $sv['quantification_id'],
//                        'quantification_user_taketime' => $sv['quantification_start_time'],
//                        'quantification_user_check' => $sv['quantification_check_cycle'],
//                        'quantification_user_addtime' => time(),
//                        'quantification_user_status' => 1,
//                    );
//                    //用户每月任务
//                    $rid = $this->M_quantification_User->saveData($data);
//                    $log.='上线任务相关模板'.$rid;
//                }
//                //修改模板状态
//                $this->M_quantification->updateData(array('quantification_status'=>2),array('quantification_id'=>$sv['quantification_id']));
//            }
//        }


/*========================================================================
        //实时刷新的上线任务  判断用户推出登录后 修改数据
        //查询所有不在线  并且 任务创建时间不超过一半的  任务 删除掉
        $quer ='(' .$time.' - start_time) < (end_time - start_time) AND user_id NOT IN('.$zaixian.')';
        $updateData = array('is_del'=>2);
        $this->M_quantification_User_Map->updateData($updateData,$quer);

        //实时刷新的上线任务  判断用户推出登录后 修改数据
        //查询所有不在线  并且 任务创建时间不超过一半的  任务 删除掉
        //$quer ='(' .$time.' - start_time) < (end_time - start_time) AND user_id NOT IN('.$zaixian.')';


        $user_map_id = 'id IN (0,0';
        $tables = array('t_quantification_user_map'=>'m','t_quantification_user'=>'q');
        $join = array('m.qu_user_id= q.id');
        $quer = 'q.statu = 1 AND q.status = 2 AND (' .$time.' - m.start_time) < (m.end_time - m.start_time) AND m.user_id NOT IN('.$zaixian.')';

        $user_list = $this->M_quantification_User_Map->getJoinList($tables,$join,'m.*',$quer,null,null);
        if(empty($user_list)){
            foreach($user_list as $sk=>$sv){
                $user_map_id .= $sv['id'];
            }
        }
        $user_map_id .= ')';
        $updateData = array('is_del'=>2);
        $this->M_quantification_User_Map->updateData($updateData,$user_map_id);


        =========================================2017-07-27==============================**/
        $user_map_id = 'quantification_user_map_id IN (0,0';
        $tables = array('t_quantification_user_map'=>'m','t_quantification_user'=>'q');
        $join = array('m.quantification_user_id= q.quantification_user_id');
//        $quer = 'q.statu = 1 AND q.status = 2 AND (' .$time.' - m.start_time) < (m.end_time - m.start_time) AND m.user_id NOT IN('.$zaixian.')';//删除判断不在线的逻辑
        $quer = 'q.quantification_user_status = 1 AND q.quantification_user_check = 2 AND ' .$time.' > m.end_time ';

        $user_list = $this->M_quantification_User_Map->getJoinList($tables,$join,'m.*',$quer,null,null);
        if(!empty($user_list)){
            foreach($user_list as $sk=>$sv){
                $user_map_id .= $sv['quantification_user_map_id'];
            }
        }
        $user_map_id .= ')';
        $updateData = array('is_del'=>2);
        $this->M_quantification_User_Map->updateData($updateData,$user_map_id);
        echo $log;

    } //已测试
    //每天按单点生成线上岗位量化任务
    public function task_start_h()
    {
        $time =time();
        $n=date("H");
        $n = intval($n);
        //获取当天的年份
        $y = date("Y");
        //获取当天的月份
        $m = date("m");
        //获取当天的号数
        $d = date("d");
        //的量化任务 未触发过的  status 1 月 2 上线  statu 1 未触犯 2 已经触发 user_type 1：人员 2：岗位
        $tables = array('t_quantification'=>'q');
        //模板
//        $moban = $this->M_quantification->getJoinList($tables,$join,'q.*,w.Name as area_name',array('q.is_del'=>1,'q.status'=>'2','q.user_type'=>'2','q.start_time <='=>$time),null,null);
        $moban = $this->M_quantification->getJoinList($tables,'','q.*',array('q.is_del'=>1,'q.quantification_check_cycle'=>'2','q.quantification_post_type'=>'3','quantification_start_time <='=>$time),null,null);
        //  $tables = array('t_user'=>'u','t_enterprise_dept'=>'b');
//        $tables = array('t_user'=>'u','t_auth_multiple'=>'b');
//        $join = array('u.user_id = b.UserID');
        if(!empty($moban)){
            foreach($moban as $sk=>$sv){
                //量化模板对应的网格任务
                $user_map = $this->M_quantification_Map->getList('*',array('quantification_id'=>$sv['quantification_id'],'is_del'=>1),null,null);
                //修改模板状态
//                    $this->M_quantification->saveData(array('id'=>$sv['id'],'statu'=>2));

//                    $user_map_r = array();
//                    $user_map_r = $this->M_quantification_User_Map->getOne('id',array('qu_map_id'=>$av['id'],'qu_user_id'=>$sv['id'],'start_time <='=>$time,'end_time >='=>$time));


                    //查询在当前岗位人
                    $tab = array('t_user'=>'u');
                    $mb = $this->M_quantification->getJoinList($tab,'','u.user_id',array('u.is_online'=>1,'u.post_id'=>$sv['quantification_user']),null,null);

                    //查询模板所对应的人员  1人员 2 岗位
//                       //循环用户给每个用户任务模板
                    foreach($mb as $skkk=>$svvv) {

                        if (empty($svvv['user_id'])) {
                            continue;
                        }
                        $rid = $this->M_quantification_User->getOne('quantification_user_id', array('user_id =' => $svvv['user_id'],'quantification_id'=>$sv['quantification_id']));

                        if(empty($rid))
                        {
                            $data = array(
                                'user_id' => $svvv['user_id'],
                                'quantification_id' => $sv['quantification_id'],
                                'quantification_user_taketime' => $sv['quantification_start_time'],
                                'quantification_user_check' => $sv['quantification_check_cycle'],
                                'quantification_user_addtime' => $time,
                                'quantification_user_status' => 1
                            );
                            //用户每月任务
                            $rid = $this->M_quantification_User->saveData($data);
                        }
//                      $log.='上线任务相关模板'.$rid;
                        foreach($user_map as $ak=>$av){
//                            $tables1 = array('t_quantification_user_map'=>'q');
//                            $user_list1 = $this->M_quantification_User_Map->getJoinList($tables1,null,'q.*',array('q.qu_user_id'=>$rid,'q.qu_map_id'=>$av['id'],'q.user_id'=>$svvv['user_id'],'q.start_time <='=>$time,'q.end_time>='=>$time),null,null);
//
//                            if(!empty($user_list1))
//                            {
//                                continue;
//                            }
//                            else
//                            {


                                //晚上1点到5点之间，2小时频率的要改为4小时频率
                                if((($n == 1) && ($av['quantification_task_cycle_h'] == 2)) || (($n == 3) && ($av['quantification_task_cycle_h'] == 2)))
                                {
                                    $av['quantification_task_cycle_h'] = 4;
                                }

                                $h = ($n-1)/$av['quantification_task_cycle_h'];
                                $h = intval($h);
                                $h = $av['quantification_task_cycle_h']*$h+1;
                                $todayTime= mktime($h,0,0,$m,$d,$y);
                                //获取当前的时间（几点$n）
                                if(($n-1)%$av['quantification_task_cycle_h'] == 0)
                                {
                                    echo "可以生成任务";
                                    //计算结束时间
                                    $map_time_i = ($av['quantification_task_cycle_d'] * 24 * 60) + ($av['quantification_task_cycle_h'] * 60);
                                    $map_time_jiange = $map_time_i * 60;// 间隔时间 秒
                                    $data = array(
                                        'quantification_user_id'=>$rid,
                                        'quantification_map_id'=>$av['quantification_map_id'],
                                        'grid_id'=>$av['grid_id'],
                                        'start_time'=>$todayTime,
                                        'end_time'=>$todayTime + $map_time_jiange,
                                        'number'=>$av['quantification_task_number'],
                                        'quantification_user_map_addtime'=>$time,
                                        'user_id'=>$svvv['user_id'],
                                        'finish_number'=>0,
                                        'type'=>1,
                                        'status'=>1,
                                        'is_del'=>1,
                                        'is_push'=>1
                                    );
                                    $p = $this->M_quantification_User_Map->saveData($data);
//                                $log.='--'.$p;
                                }
                                else
                                {
                                    echo "不可以生成任务";
                                }
//                            }
                        }
                    }
                }

            }
    }//已测试
    //每月 月初 执行
    //按月的量化任务生成
    public function task_start_m()
    {
        $time = time();
        $ys = strtotime(date('Y-m')); //月初
        $ye = date('Y-m-t') . ' 23:59:00';
        $ye = strtotime($ye);//月末
        $ys_e = $ye - $ys;     //月末和月初的时间差

        $moban = $this->M_quantification_User->getList('*', array('quantification_user_check' => 1,'quantification_user_status'=>1), null, null);
        if (empty($moban)) {
            return false;
        }

        foreach ($moban as $k => &$v) {
//            $map_ser = array();
//            $map_ser = $this->M_quantification_User_Map->getOne('id', array('end_time <=' => $time));
            //月底到触发时间的差
            $v['yes_e'] = $ys_e;
            if ($v['quantification_user_taketime']<=$time) {
                //量化任务的网格任务
                $v['map_list'] = $this->M_quantification_Map->getList('*', array('quantification_id' => $v['quantification_id']), null, null);

                //用户任务详情 根据每个任务模块的网格记录任务
                foreach ($v['map_list'] as $kkkk => $vvvv) {

                    //计算每个网格的月任务 和开始日期结束日期
                    //网格内的写的时间间隔
                    $map_time_i = ($vvvv['quantification_task_cycle_d'] * 24 * 60) + ($vvvv['quantification_task_cycle_h'] * 60);
                    $map_time_jiange = $map_time_i * 60;// 间隔时间 秒

                    $map_number = round($v['yes_e'] / $map_time_jiange);  //每个月生成任务的次数

//                    $v['user_id'] = explode(",",$v['quantification_user']);
//                    foreach($v['user_id'] as $kkk=>$vvv) {
//                        if (empty($vvv)) {
//                            continue;
//                        }
                        //每个网格的任务插入
                        for ($x = 1; $x <= $map_number; $x++) {
                            $data_v = array(
                                'quantification_user_id' => $v['quantification_user_id'],
                                'quantification_map_id' => $vvvv['quantification_map_id'],
                                'grid_id' => $vvvv['grid_id'],
                                'end_time' => $ys + ($map_time_jiange * $x)-1,
                                'start_time' => $ys + ($map_time_jiange * $x) - $map_time_jiange,
                                'finish_number' => 0,
                                'number' => $vvvv['quantification_task_number'],
                                'quantification_user_map_addtime' => $time,
                                'user_id' => $v['user_id'],
                            );
                            if ($x == $map_number) {
                                $data_v['end_time'] = $ye;
                            }
                            $this->M_quantification_User_Map->saveData($data_v);
                        }
//                    }

                }

            }

        }
    }//已测试
    //用户登录生成量化任务
    function task_login()
    {
        $pid = getParam($this->input->get_post("pid", true), 'int');//用户id
        if(empty($pid))
        {
            $d['code'] = 1;
            $d['info'] ='失败';
            exit(json_encode(array('code'=>1,'info'=>'失败！')));
        }
        $d['info'] = "成功";
        $time = time();
        //上线任务刷新
        $log='上线任务详情<br/><br/>';
        //上线的任务   每三分钟刷新一次 判断是否生成任务
        //查询上线所有有任务的人
        $tables = array('t_quantification_user'=>'q','t_user'=>'u','t_quantification'=>'qq');
        $join = array('u.user_id = q.user_id','q.quantification_id = qq.quantification_id');
        $user_list = $this->M_quantification_User_Map->getJoinList($tables,$join,'q.*',array('q.quantification_user_status'=>1,'q.quantification_user_check'=>2,'q.user_id'=>$pid,'qq.quantification_post_type<='=>2),null,null);

        $zaixian = '0,0'; //在线人id

        if(!empty($user_list)){
            //循环
            foreach($user_list as $k=>$v){
                $zaixian .= ','.$v['user_id'];
                //这个人在这个末班下要做的网格任务
                $user_map  = array();
                $user_map = $this->M_quantification_Map->getList('*',array('quantification_id'=>$v['quantification_id']),null,null);
                if(!empty($user_map)){
                    foreach($user_map as $ak=>$av){
                        //查询这个网格有没有正在执行的任务
                        $user_map_r = array();
                        $user_map_r = $this->M_quantification_User_Map->getOne('quantification_user_map_id',array('quantification_map_id'=>$av['quantification_map_id'],'quantification_user_id'=>$v['quantification_user_id'],'start_time <='=>$time,'end_time >='=>$time));
                        //没有正在执行的  添加
                        if(empty($user_map_r)){
                            //计算结束时间
                            $map_time_i = ($av['quantification_task_cycle_h'] * 24 * 60) + ($av['quantification_task_cycle_h'] * 60);
                            $map_time_jiange = $map_time_i * 60;// 间隔时间 秒
                            $data = array(
                                'quantification_user_id'=>$v['quantification_user_id'],
                                'quantification_map_id'=>$av['quantification_map_id'],
                                'grid_id'=>$av['grid_id'],
                                'start_time'=>$time,
                                'end_time'=>$time + $map_time_jiange,
                                'number'=>$av['number'],
                                'quantification_user_map_addtime'=>$time,
                                'user_id'=>$v['user_id'],
                                'finish_number'=>0,
                                'type'=>1,
                                'status'=>1,
                                'is_del'=>1,
                                'is_push'=>1
                            );
                            $p = $this->M_bjnz_new_quantification_user_map->saveData($data);
                            $d['info'] ='添加了';
                            $log.='--'.$p;
                        }
                    }
                }
            }


        }
        exit(json_encode(array('code'=>0,'info'=>$d['info'])));
        $this->printJson_data($d);

//        $user_map_id = 'id IN (0,0';
//        $tables = array('bjnz_new_quantification_user_map'=>'m','bjnz_new_quantification_user'=>'q');
//        $join = array('m.qu_user_id= q.id');
////        $quer = 'q.statu = 1 AND q.status = 2 AND (' .$time.' - m.start_time) < (m.end_time - m.start_time) AND m.PID NOT IN('.$zaixian.')';//删除判断不在线的逻辑
//        $quer = 'q.statu = 1 AND q.status = 2 AND ' .$time.' > m.end_time ';
//
//        $user_list = $this->M_bjnz_new_quantification_user_map->getJoinList($tables,$join,'m.*',$quer,null,null);
//        if(!empty($user_list)){
//            foreach($user_list as $sk=>$sv){
//                $user_map_id .= $sv['id'];
//            }
//        }
//        $user_map_id .= ')';
//        $updateData = array('isdel'=>2);
//        $this->M_bjnz_new_quantification_user_map->updateData($updateData,$user_map_id);
//        echo $log;
    }
    //每天单点生成线上量化任务（针对岗位：登岗生成）
    function task_login_post()
    {
        $pid = getParam($this->input->get_post("pid", true), 'int');//用户id
//    $pid=2642;
        $workid = getParam($this->input->get_post("workid", true), 'int');//岗位id
//    $workid=85;
        if(empty($pid) || empty($workid))
        {
            $dd['code'] = 1;
            $dd['info'] ='失败';
            exit(json_encode(array('code'=>1,'info'=>'失败！')));
        }
        $sql = "select q.* from t_quantification q  where q.quantification_post_type=3 and q.is_del=1 and q.quantification_check_cycle=2 and q.quantification_user=$workid";
        $quanti = $this->M_quantification_User_Map->querylist($sql);

        if(count($quanti)==0)
        {
            exit(json_encode(array('code'=>1,'info'=>'当前岗位无任务！')));
        }
        $dd['info'] = "成功";
        $time = time();
        $n=date("H");
        $n = intval($n);
        //获取当天的年份
        $y = date("Y");
        //获取当天的月份
        $m = date("m");
        //获取当天的号数
        $d = date("d");
        //上线的任务   每天单点 判断是否生成任务
        $tables = array('t_quantification'=>'q','t_quantification_user'=>'u');
        $join = array('q.quantification_id=u.quantification_id');
        $user_list = $this->M_quantification_User_Map->getJoinList($tables,$join,'q.*,u.quantification_user_id as userid',array('q.quantification_post_type'=>3,'q.is_del'=>1,'q.quantification_check_cycle'=>2,'u.user_id'=>$pid,'q.quantification_user'=>$workid),null,null);


        if(!empty($user_list)){
            //已生成过当前PID任务
            foreach($user_list as $k=>$v){
                //这个人在这个末班下要做的网格任务
                $user_map  = array();
                $sql = "select * from t_quantification_map where quantification_id=".$v['id'];
                $user_map = $this->M_quantification_User_Map->querylist($sql);
                if(!empty($user_map)){
                    foreach($user_map as $ak=>$av){
                        //查询当前时间是否已有任务（当前时间段的任务）
                        $tables1 = array('t_quantification_user_map'=>'q');
                        $user_list1 = $this->M_quantification_User_Map->getJoinList($tables1,null,'q.*',array('q.quantification_user_id'=>$v['userid'],'q.quantification_map_id'=>$av['quantification_map_id'],'q.grid_id'=>$av['grid_id'],'q.user_id'=>$pid,'q.start_time <='=>$time,'q.end_time>='=>$time),null,null);

                        if(!empty($user_list1))//有当前时间段的任务并且离结束时间大于半小时才会生成任务
                        {
                            continue;
                        }
                        else
                        {

                            //晚上1点到5点之间，2小时频率的要改为4小时频率
                            if((($n >= 1) && ($av['quantification_task_cycle_h'] == 2)) && (($n <=4) && ($av['quantification_task_cycle_h'] == 2)))
                            {
                                $av['quantification_task_cycle_h'] = 4;
                            }

//                        $sql = "SELECT id FROM bjnz_new_quantification_user_map WHERE qu_user_id=".$v['userid']." and qu_map_id=".$av['map_id']." and PID=".$pid;
//                        $count = $this->M_bjnz_new_quantification_user_map->querylist($sql);
//                        $count = count($count)+1;
                            $h = ($n-1)/$av['quantification_task_cycle_h'];
                            $h = floor($h);
//                        $h = intval($h);
                            $h = $av['quantification_task_cycle_h']*$h+1;
//                        $h=($av['h']*$count)+1;
                            $todayTime= mktime($h,0,0,$m,$d,$y);
                            //计算结束时间
                            $map_time_i = ($av['quantification_task_cycle_d'] * 24 * 60) + ($av['quantification_task_cycle_h'] * 60);
                            $map_time_jiange = $map_time_i * 60;// 间隔时间 秒
                            $data = array(
                                'quantification_user_id'=>$v['userid'],
                                'quantification_map_id'=>$av['id'],
                                'grid_id'=>$av['map_id'],
                                'start_time'=>$todayTime,
                                'end_time'=>$todayTime + $map_time_jiange,
                                'number'=>$av['number'],
                                'quantification_user_map_addtime'=>$time,
                                'user_id'=>$pid,
                                'finish_number'=>0,
                                'type'=>1,
                                'status'=>1,
                                'is_del'=>1,
                                'is_push'=>1
                            );
//                        if($data['end_time']-$time < 1800)
//                        {
//                            continue;
//                        }
//                        else
//                        {
                            $p = $this->M_quantification_User_Map->saveData($data);
                            $dd['info'] = '添加了';
//                                echo "添加";
//                            }
//                            else
//                            {
//                            }
//                        }
                        }
                    }
                }
            }
        }
        else
        {
            foreach ($quanti as $value)
            {
                //还没有生成当前PID的任务
                $data = array(
                    'user_id' =>$pid,
                    'quantification_id' => $value['id'],
                    'quantification_user_taketime' => $value['quantification_user_taketime'],
                    'quantification_user_check' => $value['quantification_check_cycle'],
                    'quantification_user_addtime' => $time,
                    'quantification_user_status' => 1
                );
                //用户每月任务
                $rid = $this->M_quantification_User->saveData($data);
                $user_map  = array();
                $sql = "select * from t_quantification_map where quantification_id=".$value['quantification_id'];
                $user_map = $this->M_quantification_User_Map->querylist($sql);
                foreach($user_map as $ak=>$av){
                    //晚上1点到5点之间，2小时频率的要改为4小时频率
                    if((($n >= 1) && ($av['quantification_task_cycle_h'] == 2)) && (($n <=4) && ($av['quantification_task_cycle_h'] == 2)))
                    {
                        $av['quantification_task_cycle_h'] = 4;
                    }

                    $h = ($n-1)/$av['quantification_task_cycle_h'];
                    $h = floor($h);
//              $h = intval($h);
                    $h = $av['quantification_task_cycle_h']*$h+1;

                    $todayTime= mktime($h,0,0,$m,$d,$y);
                    //获取当前的时间（几点$n）
                    //计算结束时间
                    $map_time_i = ($av['quantification_task_cycle_d'] * 24 * 60) + ($av['quantification_task_cycle_h'] * 60);
                    $map_time_jiange = $map_time_i * 60;// 间隔时间 秒
                    $data = array(
                        'quantification_user_id'=>$rid,
                        'quantification_map_id'=>$av['quantification_map_id'],
                        'grid_id'=>$av['grid_id'],
                        'start_time'=>$todayTime,
                        'end_time'=>$todayTime + $map_time_jiange,
                        'number'=>$av['number'],
                        'quantification_user_map_addtime'=>$time,
                        'user_id'=>$pid,
                        'finish_number'=>0,
                        'type'=>1,
                        'status'=>1,
                        'is_del'=>1,
                        'is_push'=>1
                    );

//                if($data['end_time']-$time < 1800)
//                {
//                    continue;
//                }
//                else
//                {
                    $p = $this->M_quantification_User_Map->saveData($data);
                    $dd['info'] = '添加了';
//                }
//            echo "添加";
                }
            }
        }
        exit(json_encode(array('code'=>0,'info'=>$dd['info'])));
        $this->printJson_data($dd);
    }

}
